package com.springboot.activemq.producter.demo.code;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 及时消息 与 延迟消息
 */
@RestController
public class TimelyOrDelayMessageController {
    private final ActiveMQHandler handler;

    @Autowired
    public TimelyOrDelayMessageController(ActiveMQHandler handler) {
        this.handler = handler;
    }

    /**
     * 及时 消息
     * @return
     */
    @RequestMapping("/timelySend")
    public String timelySend(){
        // 构建一个消息, 名称是 queue01
        for(int i=0;i<10;i++){
            String message = "我是及时消息内容, " + i;
            handler.send("queue01", message);
        }

        return "success";
    }

    /**
     * 延迟消息，需要修改activemq 配置文件activemq.xml 中的borker 标签开启延迟功能
     * @return
     */
    @RequestMapping("/delaySend")
    public String delaySend(){
        // 构建一个消息, 名称是 queue01
        for (int i = 0; i < 10; i++) {
            String message = "我是延迟消息内容, " + i;
            handler.delaySend("delaySend01",message,10);//延迟10秒后，消费者才能接收到消息。
        }
        return "success";
    }

    /**
     * 订阅者模式消息
     * @return
     */
    @RequestMapping("/topic")
    public String topic(){
        handler.topic("topic01","hello world");
        return "success";
    }
}
